package controllers

import (
	"ginDev/frans"
	"ginDev/models"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/gorm"
)

type ArticleController struct {
	*frans.GormAdapter
}

func NewArticleController() *ArticleController {
	return &ArticleController{}
}

func (this *ArticleController) Route(frans *frans.Frans) {
	frans.Handle("GET", "/art", this.Index).
		Handle("GET", "/art/1", this.Detail)
}

func (this *ArticleController) Index(ctx *gin.Context) frans.Model {
	//return "index"

	article := models.ArticleModel{}
	this.Table("article").Where("id=?", 1).Find(&article)

	return &article
}

func (this *ArticleController) Detail(ctx *gin.Context) frans.Model {

	article := models.ArticleModel{}
	this.Table("article").Where("id=?", 1).Find(&article)

	frans.Task(this.UpdateViews, article.Id)

	return &article
}

//更新查看次数
func (this *ArticleController) UpdateViews(params ...interface{}) {
	this.Table("article").
		Where("id=?", params[0]).
		Update("views", gorm.Expr("views+1"))
}
